<script setup>
import { ref, reactive } from 'vue';
import { ElMessage } from 'element-plus';
import { User, Lock } from '@element-plus/icons-vue';
import request from '@/utils/axios';
import { useRouter } from 'vue-router';

const router = useRouter();

const form = reactive({
  account: '',
  password: ''
});

const rules = {
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 20, message: '账号长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ]
};

const loginFormRef = ref(null);
const loading = ref(false);

const handleLogin = async () => {
  try {
    await loginFormRef.value.validate();
    
    loading.value = true;
    
    const response = await request.post('/api/login', {
      username: form.account,
      password: form.password
    });
    
    if (response.data.code === 200) {
      ElMessage.success('登录成功');
      
      localStorage.setItem('token', response.data.data.token);
      localStorage.setItem('userId', response.data.data.userId);
      
      router.push('/');
    } else {
      ElMessage.error(response.data.message || '登录失败');
    }
  } catch (error) {
    if (error.response) {
      ElMessage.error(error.response.data?.message || '登录失败');
    } else if (error.request) {
      ElMessage.error('网络错误，请检查服务是否启动');
    } else {
      ElMessage.error('登录失败：' + error.message);
    }
  } finally {
    loading.value = false;
  }
};
</script>

<template>
  <el-row class="login-page" justify="center">
    <el-col :span="16" class="left-side">
      <div class="content">
      </div>
    </el-col>
    
    <el-col :span="8" class="right-side">
      <div class="login-box">
        <div class="logo">LOGO</div>
        <h2>欢迎登录</h2>
        <el-form 
          ref="loginFormRef" 
          :model="form" 
          :rules="rules" 
          status-icon
          label-width="0px"
        >
          <el-form-item prop="account">
            <el-input 
              v-model="form.account" 
              placeholder="请输入账号"
              clearable
            >
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              type="password"
              placeholder="请输入密码"
              clearable
            >
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item>
            <el-button 
              type="primary" 
              block 
              @click="handleLogin"
              :loading="loading"
              style="width: 100%"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
        
        <div class="other-options">
          <a href="#" class="register-link">没有账号？去注册</a>
          <a href="#" class="forgot-link">忘记密码？</a>
        </div>
      </div>
      <div class="copyright">
        AI角色扮演-七牛云校招项目 ©2025拉拉队
      </div>
    </el-col>
  </el-row>
</template>

<style scoped>
.login-page {
  height: 100vh;
}

.left-side {
  background-image: url('@/assets/images/loginBg.jpg'); 
  background-size: cover;
  background-position: center;
  color: #fff;
  position: relative;
}

.logo {
  width: 100px;
  height: 100px;
  text-indent: -999px;
  overflow: hidden;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: 20px;
  font-size: 28px;
  font-weight: bold;
  margin-bottom: 60px;
  background-image: url('@/assets/images/logo.png'); 
  background-size: cover;
  background-position: center;
}

.copyright {
  position: absolute;
  bottom: 5px;
  font-size: 12px;
  opacity: 0.8;
}

.right-side {
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-box {
  width: 350px;
  padding: 40px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.login-box h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-weight: 600;
}

.other-options {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
  font-size: 14px;
}

.other-options a {
  color: #409eff;
  text-decoration: none;
}

.other-options a:hover {
  color: #66b1ff;
  text-decoration: underline;
}
</style>